package com.faithbj.controller.compare;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.model.User;
import com.faithbj.common.web.controller.BaseController;
import com.faithbj.service.CheckTotalService;
import com.faithbj.service.CompareService;
import com.faithbj.support.compare.AjaxResult;
import com.faithbj.support.updownload.FileUploadHandler;

/**
 * 电子比对交换Controller，
 * <p>目前有文件上传、文件下载和手动批次生成三个method</p>
 * 后续根据业务需求可以做拆分
 * @author chen liu
 *
 */
@Controller
@RequestMapping("/compare/*")
public class CompareController extends BaseController{
	
	@Resource
	private FileUploadHandler uploadHandler;
	
	@Resource
	private CheckTotalService ctService;
	
	@Resource
	private CompareService compareService;
	
	/**
	 * 导出数据文件(data.zip)
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/exportDBFile")
	public void exportDBFile(HttpServletRequest request, ModelMap model) {
		
	}
	
	/**
	 * 导入数据文件(data.zip)
	 * @param request
	 * @param id 受理编号
	 * @param model
	 * @return AjaxResult
	 */
	@RequestMapping("/importDBFile")
	@ResponseBody
	public AjaxResult importDBFile(HttpServletRequest request, String id,  ModelMap model) {
		AjaxResult result = new AjaxResult(true);
		String filePath = null;
		if (request instanceof MultipartHttpServletRequest) {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile("dbData");
			
			// 检测扩展名
			String fileName = file.getOriginalFilename();
			if(fileName != null){
				 fileName = fileName.toLowerCase();
                 int lastIndex = fileName.lastIndexOf(".");
                 if (lastIndex != -1) {
                     String fileExt = fileName.substring(lastIndex, fileName.length());
                     if(StringUtils.hasLength(fileExt) && ".zip".equals("fileExt")){
                    	 filePath = uploadHandler.uploadByFilePath(file, "F:\\data.zip");
                     }
                 }
			}
		}
		if(filePath == null){
			result.setResult(false);
			result.addMsg("数据文件上传失败!");
		}
		return result;
	}
	
	/**
	 * 人工生成批次
	 * @param request
	 * @param acceptNums 受理编号数组
	 * @param model
	 * @return
	 */
	@RequestMapping("/generateBatch")
	@ResponseBody
	public AjaxResult generateBatch(HttpServletRequest request, String[] acceptNums, ModelMap model){
		AjaxResult result = new AjaxResult(true);
		
		// 参数验证
		if(acceptNums == null || acceptNums.length == 0){
			result.setResult(false);
			result.addMsg("受理编号不合法，请确认!");
		}
		
		// 获取用户进行权限验证？？如何验证
		User user = super.getLoginUser();
		
		// 根据受理编号查询核对总体情况记录
		Map<String, List<CheckTotal>> batchingMap = ctService.getCheckTotalMapForBatching(acceptNums);
		
		// 生成批次,发送委办
		String resultMsg = compareService.genearateBatch(batchingMap);
		if(resultMsg != null){
		    result.setResult(false);
		    result.addMsg(resultMsg);
		}
		
		return result;
		
	}
}
